package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.core.toolkit.StringToolkit;
import info.thereisonlywe.quran.text.QuranicTextList;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;

public class CopyDialog {

	private static JDialog dialog;
	private static JCheckBox checkBox_1;
	private static JCheckBox checkBox;
	private static JCheckBox chckbxNewCheckBox_1;
	private static JComboBox<String> comboBox_1;
	private static JComboBox<String> comboBox = new JComboBox<>();
	private static JCheckBox chckbxNewCheckBox;
	private static JButton okButton;
	private static JCheckBox checkBox_2;
	private static int callingRow = -1;
	private static final JRadioButton rdbtnNewRadioButton = new JRadioButton(
			ProjectLocalization.getString(
					ProjectConstants.ONENESS_PATHFINDER_PROJECT,
					"Copy.SectionNumber"));
	private static final JRadioButton radioButton = new JRadioButton(
			ProjectLocalization.getString(
					ProjectConstants.ONENESS_PATHFINDER_PROJECT,
					"Copy.SectionName"));
	private static final JCheckBox checkBox_3 = new JCheckBox(
			ProjectLocalization.getString(ProjectConstants.CORE_PROJECT,
					"RemoveAccentsAndDiacritics"));

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setIconImage(null);
		dialog.setTitle(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "Copy"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setResizable(false);
		dialog.setPreferredSize(new Dimension(370, 290));
		dialog.setSize(new Dimension(370, 315));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);
		okButton = new JButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "OK"));
		okButton.setActionCommand("OK");
		dialog.getRootPane().setDefaultButton(okButton);
		buttonPane.add(okButton);
		JPanel panel = new JPanel();
		dialog.getContentPane().add(panel, BorderLayout.CENTER);

		rdbtnNewRadioButton.setSelected(true);
		ButtonGroup bg = new ButtonGroup();
		bg.add(rdbtnNewRadioButton);
		bg.add(radioButton);
		JPanel panel_1 = new JPanel();

		chckbxNewCheckBox_1 = new JCheckBox(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"Copy.AddVerseInfo"));
		chckbxNewCheckBox_1.setSelected(true);

		JPanel panel_2 = new JPanel();
		panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

		checkBox = new JCheckBox(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "Copy.WrapInfo")
				+ "   ");
		checkBox.setSelected(true);
		panel_2.add(checkBox);

		checkBox_1 = new JCheckBox(
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Copy.AddTextInfo"));
		checkBox_1.setSelected(true);

		checkBox_2 = new JCheckBox(
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Copy.AddQuranTag"));
		checkBox_2.setSelected(true);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addComponent(
																		panel_1,
																		GroupLayout.DEFAULT_SIZE,
																		352,
																		Short.MAX_VALUE)
																.addGap(6))
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.TRAILING)
																				.addComponent(
																						checkBox_2,
																						Alignment.LEADING,
																						GroupLayout.DEFAULT_SIZE,
																						352,
																						Short.MAX_VALUE)
																				.addComponent(
																						chckbxNewCheckBox_1,
																						GroupLayout.DEFAULT_SIZE,
																						352,
																						Short.MAX_VALUE))
																.addContainerGap())
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addGroup(
																		gl_panel.createParallelGroup(
																				Alignment.LEADING)
																				.addGroup(
																						gl_panel.createSequentialGroup()
																								.addComponent(
																										checkBox_1,
																										GroupLayout.DEFAULT_SIZE,
																										351,
																										Short.MAX_VALUE)
																								.addGap(1))
																				.addComponent(
																						panel_2,
																						GroupLayout.DEFAULT_SIZE,
																						352,
																						Short.MAX_VALUE))
																.addContainerGap())
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addGap(3)
																.addComponent(
																		rdbtnNewRadioButton,
																		GroupLayout.PREFERRED_SIZE,
																		319,
																		GroupLayout.PREFERRED_SIZE)
																.addContainerGap())
												.addGroup(
														Alignment.TRAILING,
														gl_panel.createSequentialGroup()
																.addComponent(
																		radioButton,
																		GroupLayout.PREFERRED_SIZE,
																		319,
																		GroupLayout.PREFERRED_SIZE)
																.addContainerGap())
												.addGroup(
														gl_panel.createSequentialGroup()
																.addComponent(
																		checkBox_3,
																		GroupLayout.DEFAULT_SIZE,
																		351,
																		Short.MAX_VALUE)
																.addGap(7)))));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(checkBox_2, GroupLayout.PREFERRED_SIZE,
								24, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(chckbxNewCheckBox_1,
								GroupLayout.PREFERRED_SIZE, 24,
								GroupLayout.PREFERRED_SIZE)
						.addGap(3)
						.addComponent(rdbtnNewRadioButton,
								GroupLayout.PREFERRED_SIZE, 22,
								GroupLayout.PREFERRED_SIZE)
						.addGap(2)
						.addComponent(radioButton, GroupLayout.PREFERRED_SIZE,
								22, GroupLayout.PREFERRED_SIZE)
						.addGap(10)
						.addComponent(checkBox_1, GroupLayout.PREFERRED_SIZE,
								26, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(checkBox_3, GroupLayout.PREFERRED_SIZE,
								26, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(28, Short.MAX_VALUE)));

		panel_2.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {
				"( )", "{ }", "[ ]" }));
		panel_1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

		chckbxNewCheckBox = new JCheckBox(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"Copy.EncloseInQuotationMarks")
				+ "   ");
		chckbxNewCheckBox.setSelected(true);
		panel_1.add(chckbxNewCheckBox);

		comboBox_1 = new JComboBox<>();
		panel_1.add(comboBox_1);
		comboBox_1.setModel(new DefaultComboBoxModel<String>(new String[] {
				"\" \"", "' '" }));
		panel.setLayout(gl_panel);
		initListeners();
	}

	private static void processSettings()
	{
		StringBuilder sb = new StringBuilder();
		if (chckbxNewCheckBox.isSelected())
		{
			sb.append(((String) comboBox_1.getSelectedItem()).charAt(0));
		}
		String s = ((String) MainFrame.view_Browser.getVersePane().getTable()
				.getValueAt(callingRow, 0)).trim();

		sb.append(checkBox_3.isSelected() ? StringToolkit.removeDiacritics(s)
				: s);
		if (chckbxNewCheckBox.isSelected())
		{
			sb.append(((String) comboBox_1.getSelectedItem()).charAt(2));
		}
		if (checkBox_1.isSelected() || chckbxNewCheckBox_1.isSelected())
		{
			sb.append(" ");
			if (checkBox.isSelected())
			{
				sb.append(((String) comboBox.getSelectedItem()).charAt(0));
			}
			if (checkBox_2.isSelected())
			{
				sb.append(ProjectLocalization.getString(
						ProjectConstants.QURAN_PROJECT, "Quran"));
				sb.append(" ");
			}
			if (chckbxNewCheckBox_1.isSelected())
			{
				if (!radioButton.isSelected()) sb.append(UIDelegate
						.getDisplayedVerse().toNotationString());
				else
				{
					sb.append(UIDelegate.getDisplayedVerse()
							.toLongNotationString());
				}
				if (checkBox_1.isSelected() && callingRow != 0)
				{
					sb.append(" - ");
				}
			}
			if (checkBox_1.isSelected() && callingRow != 0)
			{
				sb.append(QuranicTextList
						.getText(
								QuranicTextList.getIndexByDisplayOrder(
										callingRow - 1,
										UIDelegate.getDisplayedVerse()))
						.getIdentifier().getAuthor());
			}

			if (checkBox.isSelected())
			{
				sb.append(((String) comboBox.getSelectedItem()).charAt(2));
			}
		}
		GUIToolkit.setClipboardContents(sb.toString());
	}

	private static void initListeners()
	{

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
							{
								dialog.setVisible(false);
								UIDelegate.setOpenDialog(false);
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

		okButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				processSettings();
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

		checkBox.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (checkBox.isSelected()) comboBox.setEnabled(true);
				else comboBox.setEnabled(false);
			}
		});

		chckbxNewCheckBox_1.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (chckbxNewCheckBox_1.isSelected())
				{
					radioButton.setEnabled(true);
					rdbtnNewRadioButton.setEnabled(true);
				}
				else
				{
					radioButton.setEnabled(false);
					rdbtnNewRadioButton.setEnabled(false);
				}
			}
		});

		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

	}

	public static void showDialog(int callingRow)
	{
		init();
		CopyDialog.callingRow = callingRow;
		if (callingRow != 0) checkBox_1.setEnabled(true);
		else checkBox_1.setEnabled(false);
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		UIDelegate.setOpenDialog(true);
		dialog.setVisible(true);
	}

}
